﻿define([
    "dojo/_base/declare"
    , "esri/layers/VectorTileLayer"
], function (declare
    , VectorTileLayer) {
    var vectorTileLayer = declare([VectorTileLayer], {

        constructor: function (options) {
            //this.inherited(arguments, [options]);

            this._map = null; //地图对象

        }

        , _setMap: function (map, svg) {
            var me = this;
            var g = this.inherited(arguments);

            me._map = map;

            return g;
        }

        , _unsetMap: function (map, svg) {
            var me = this;
            me.inherited(arguments);
            if (me._clickHandle) {
                me._clickHandle.remove()
                me._clickHandle = null;
            }
            if (map._dyClickHandleObj && map._dyClickHandleObj[me.id]) {
                delete   map._dyClickHandleObj[me.id];
            }
            ;

            me._map = null;
        }
        , setData: function (mapView, layer) {
            var me = this;
            me._mapView = mapView;
            var mapUtils = mapView.mapUtils;
            me._mapUtils = mapUtils;
            var map = mapView.getMap();
            if (!mapView) {
                return;
            }
            ;
            if (!mapUtils) {
                return;
            }
            ;

            var options = layer.layer || {};
            me._options = options;
            if (typeof options.callback == 'function') {
                options.callback(layer);
            }
            ;
            if (options && options.onClick) {
                me._clickHandle = map.on('click', function (e) {
                    if (me.visible) {
                        me.clickEvent(e);
                    }
                    ;
                });
                if (options.identifyUrl) {
                    map._dyClickHandleObj = map._dyClickHandleObj || {};
                    map._dyClickEvtObj = map._dyClickEvtObj || {};
                    map._dyClickHandleObj[me.id] = me._clickHandle;
                }
                ;
            }
            ;
        },
        clickEvent: function (e) {
            var me = this;
            var map = me._map;
            var pt = me._mapUtils.createPoint(me._mapView, {x: e.mapPoint.x, y: e.mapPoint.y});
            e.getLocation = function () {
                return me.getGraphicLocation(me._mapView, e);
            };
            if (me._options.identifyUrl) {
                var _dyClickHandleKeys = Object.keys(map._dyClickHandleObj);
                var _dyClickHandleLen = _dyClickHandleKeys.length;
                me._mapUtils._currentClickLayer = me.id;
                me._mapUtils.identifyTask(me._mapView, me._options.identifyUrl, {
                    geometry: pt,
                    callback: function (evt) {
                        if (!map._clickHandleCount) {
                            map._clickHandleCount = 1;
                        } else {
                            map._clickHandleCount++;
                        }
                        ;

                        evt.mapView = me._mapView;

                        map._dyClickEvtObj[me.id] = evt;

                        if (map._clickHandleCount == _dyClickHandleLen) {
                            map._clickHandleCount = 0;
                            var _clickLayerNames = Object.keys(map._dyClickEvtObj);
                            for (let i = _clickLayerNames.length - 1; i >= 0; i--) {
                                var _layerName = _dyClickHandleKeys[i];
                                var e = map._dyClickEvtObj[_layerName];
                                if (e && e.features && e.features.length > 0) {
                                    var _layer = map.getLayer(_layerName);
                                    if (_layer && _layer.visible) {
                                        _layer._options.onClick(e);
                                        break;
                                    }
                                    ;
                                }
                                ;
                            }
                            ;
                            map._dyClickEvtObj = {};
                        }
                    },
                    ...me._options.identifyOptions
                });
            } else if (me._options.queryUrl) {
                me._mapUtils.queryTask(me._mapView, me._options.queryUrl, {
                    geometry: pt,
                    callback: function (evt) {
                        evt.getLocation = e.getLocation;
                        me._options.onClick(evt);
                    },
                    ...me._options.queryOptions
                })
            }


        },
        getGraphicLocation: function (mapView, esriEvent) {
            var geometry = esriEvent.mapPoint;
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        }
    });
    return vectorTileLayer;
});